package com.ruoyi.business.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.business.domain.AuthBindingInfo;
import com.ruoyi.business.domain.vo.WxUserVO;
import com.ruoyi.business.service.IAuthBindingInfoService;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WxUserMapper;
import com.ruoyi.business.domain.WxUser;
import com.ruoyi.business.service.IWxUserService;

/**
 * 微信用户Service业务层处理
 * 
 * @author asher
 * @date 2021-11-06
 */
@Service
public class WxUserServiceImpl implements IWxUserService 
{
    @Autowired
    private WxUserMapper wxUserMapper;

    @Autowired
    private IAuthBindingInfoService authBindingInfoService;

    /**
     * 查询微信用户
     * 
     * @param userId 微信用户主键
     * @return 微信用户
     */
    @Override
    public WxUser selectWxUserByUserId(Long userId)
    {
        return wxUserMapper.selectWxUserByUserId(userId);
    }

    /**
     * 查询微信用户列表
     * 
     * @param wxUser 微信用户
     * @return 微信用户
     */
    @Override
    public List<WxUser> selectWxUserList(WxUser wxUser)
    {
        return wxUserMapper.selectWxUserList(wxUser);
    }

    /**
     * 新增微信用户
     * 
     * @param wxUser 微信用户
     * @return 结果
     */
    @Override
    public int insertWxUser(WxUser wxUser)
    {
        wxUser.setCreateTime(DateUtils.getNowDate());
        return wxUserMapper.insertWxUser(wxUser);
    }

    /**
     * 修改微信用户
     * 
     * @param wxUser 微信用户
     * @return 结果
     */
    @Override
    public int updateWxUser(WxUser wxUser)
    {
        wxUser.setUpdateTime(DateUtils.getNowDate());
        return wxUserMapper.updateWxUser(wxUser);
    }

    /**
     * 批量删除微信用户
     * 
     * @param userIds 需要删除的微信用户主键
     * @return 结果
     */
    @Override
    public int deleteWxUserByUserIds(Long[] userIds)
    {
        return wxUserMapper.deleteWxUserByUserIds(userIds);
    }

    /**
     * 删除微信用户信息
     * 
     * @param userId 微信用户主键
     * @return 结果
     */
    @Override
    public int deleteWxUserByUserId(Long userId)
    {
        return wxUserMapper.deleteWxUserByUserId(userId);
    }

    @Override
    public int handleLogin(WxUser wxUser) {
        WxUser currentWxUser = selectWxUserByOpenId(wxUser.getOpenid());
        if (currentWxUser == null) {
            return insertWxUser(wxUser);
        }
        Date nowDate = DateUtils.getNowDate();
        currentWxUser.setUserName(wxUser.getUserName());
        currentWxUser.setLanguage(wxUser.getLanguage());
        currentWxUser.setAvatar(wxUser.getAvatar());
        currentWxUser.setNickName(wxUser.getNickName());
        currentWxUser.setProvince(wxUser.getProvince());
        currentWxUser.setCity(wxUser.getCity());
        currentWxUser.setCountry(wxUser.getCountry());
        currentWxUser.setRecentLoginDate(nowDate);
        currentWxUser.setUpdateTime(nowDate);
        return updateWxUser(currentWxUser);
    }

    @Override
    public WxUser selectWxUserByOpenId(String openId) {
        return wxUserMapper.selectWxUserByOpenId(openId);
    }

    @Override
    public WxUserVO selectWxUserWithAuthByUserId(Long userId) {
        WxUser wxUser = selectWxUserByUserId(userId);
        WxUserVO wxUserVO = new WxUserVO();
        BeanUtils.copyProperties(wxUser, wxUserVO);
        List<AuthBindingInfo> authBindingInfos = authBindingInfoService.selectAuthBindingInfoByWxUserIdAndStatus(wxUserVO.getUserId(), "2");
        wxUserVO.setBindings(authBindingInfos);
        return wxUserVO;
    }
}
